//
//  ExpenseViewController.m
//  iMoney
//
//  Created by VertigoSol on 9/12/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "ExpenseViewController.h"
#import "Expense.h"
#import "ExpenseCell.h"
#import "iMoneyAppDelegate.h"
#import "ExpenseEditController.h"

@implementation ExpenseViewController

@synthesize account;


-(id) initWithAccount:(Account*) acc
{
	if(self = [super init])
	{
		self.account = acc;
	}
	
	return self;
}


// Implement viewDidLoad to do additional setup after loading the view.
- (void)viewDidLoad {
  	


//	UIBarButtonItem *editButton =
//	[[UIBarButtonItem alloc]
//	 initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
//	 target:self
//	 action:@selector(editClicked:)];
//	self.navigationItem.leftBarButtonItem  = nil;
//	self.navigationItem.rightBarButtonItem = editButton;
//	[editButton release];

	
		
	//[self.view setBackgroundColor:[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"gradientBackground.png"]]];
	
	//UIImageView* bImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gradientBackground.png"]];
		
	CGRect frame = [[UIScreen mainScreen] applicationFrame];	
	self.view= [[[UIView alloc] initWithFrame:frame] autorelease];
	self.view.autoresizesSubviews = YES;
	
	aTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) style: UITableViewStylePlain];
	
	aTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
	aTableView.rowHeight = 60;
	aTableView.backgroundColor = [UIColor lightGrayColor];
	
	aTableView.dataSource = self;
	aTableView.delegate = self;
	
	aTableView.allowsSelectionDuringEditing = NO;
	aTableView.autoresizesSubviews= YES;
	
	[self.view addSubview:aTableView];
	//[self.view addSubview:bImage];
	//[self.view sendSubviewToBack:bImage];
	
	//[bImage release];
}


-(IBAction) editClicked:(id)sender {
		    
	
	if(aTableView.editing){
		[aTableView setEditing:NO animated:YES];
	}
	else
		[aTableView setEditing:YES animated:YES];
	
	
	[aTableView reloadData];
	
	
		   
	}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	
	//NSMutableDictionary* d = [[sharedMoneyState.accounts objectAtIndex:0] getExpensesAsDateDic];
//	
//	NSEnumerator *enumerator = [d keyEnumerator];
//	
//	id key;
//	
//	while ((key = [enumerator nextObject])) {
//		
//		for(Expense* e in [d valueForKey:key])
//		{
//			NSLog(@"key: %@, value: %@", key, e.ename);
//		}
//		
//	}
//	
//	[d release];
	
	
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	
	if(self.editing)
	{
		return account.count+1;
	}
	
    return [account count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
	static NSString *CellIdentifier = @"AccountCell";
	
	ExpenseCell *cell = (ExpenseCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
	if (cell == nil) {
		cell = [[[ExpenseCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; // changed this
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	}


	if([aTableView isEditing])
	{
		
		[cell makeAddRow: NO];
		if(indexPath.row == 0)
		{
			[cell makeAddRow: YES];
			return cell;
		}
	

		//Expense* e = [[account expenses] objectAtIndex: ([account count]-1) - indexPath.row-1]; //reverse list
		
		//get values from accoutn to display
		Expense* e = [[account expenses] objectAtIndex: indexPath.row-1];
		[cell setVals:[e ename] :[e type] :[e amount] : [e date]];
	
	}
	else
	{
	
		[cell makeAddRow: NO];
		Expense* e = [[account expenses] objectAtIndex: indexPath.row];	
		[cell setVals:[e ename] :[e type] :[e amount] : [e date]];
	
	}
		
    // Configure the cell
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
   	ExpenseEditController* ec = [[ExpenseEditController alloc] initWithNibName:@"ExpenseEditController" bundle:[NSBundle mainBundle]];

	[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:[ec view] cache:NO];
	[ec setValues:[[account expenses] objectAtIndex:indexPath.row]];
	[[self navigationController] pushViewController:ec animated:YES];

	[ec release];
	
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return UITableViewCellEditingStyleDelete;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
		
		//remove expense from model
		[account removeExpenseAtIndex:indexPath.row];
		
		// Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
		
    }
    //if (editingStyle == UITableViewCellEditingStyleInsert) {
//		
//		Expense* e = [[Expense alloc] initAll:[NSDate date] :@"-" :@"-" :0];
//		[account addExpense:e :YES];	
//		
//		ExpenseEditController* ec = [[ExpenseEditController alloc] initWithNibName:@"ExpenseEditController" bundle:[NSBundle mainBundle]];
//		
//		//[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:[ec view] cache:NO];
//		[ec setValues:e];
//		[[self navigationController] pushViewController:ec animated:YES];
//		[ec release];
//    }
}


- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
	
	//[super setEditing:editing animated:animated];
	
	NSArray *indexPaths = [NSArray arrayWithObjects:
						   [NSIndexPath indexPathForRow:0 inSection:0]];
	

	if (editing) {
        // Show the placeholder row
       [aTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];
    } else {
        // Hide the placeholder row.
        [aTableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];
    }
	
	[aTableView reloadData];
}




 - (void)viewDidDisappear:(BOOL)animated {
	 
	 
 }
 

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	[self.account updateAmount];
	[aTableView reloadData]; //reload table
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (void)dealloc {
	[aTableView release];
	[account release];
    [super dealloc];
}


@end

